Ext.define('Qms.view.desktop.DesktopViewport', {
    extend: 'Ext.container.Viewport',
    id: 'desktopViewport',
    layout: {
        type: 'border'
    },

    startMenuOn: function(){
        var startMenu = Ext.getCmp('startMenuPanel');
        var startButton = Ext.getCmp('startMenuButton');
        startMenu.setVisible(true);
        startButton.toggle(true);
    },
    
    startMenuOff: function(){
        var startMenu = Ext.getCmp('startMenuPanel');
        var startButton = Ext.getCmp('startMenuButton');
        startMenu.setVisible(false);
        startButton.toggle(false);
    },
    
    setStartMenuPosition: function(x, y, animate){
        var startMenu = Ext.getCmp('startMenuPanel');
        startMenu.setPosition(x, y-(310+30), animate);
    },
    
    getViewportHeight: function(){
        var viewportHeight = Ext.getCmp('desktopViewport').getHeight();
        return viewportHeight;
    },
    
    getViewportWidth: function(){
        var viewportWidth = Ext.getCmp('desktopViewport').getWidth();
        return viewportWidth;
    },
    
    getLoginInfo: function(){
      Ext.Ajax.request({
        url: 'login/loginInfo',
        method: 'GET',
        success: function(result, request){
            
            var responseData = result.responseText;
            var jsonData = Ext.JSON.decode(responseData); 
            
            var startMenu = Ext.getCmp('startMenuPanel');
            var companyName = Ext.getCmp('companyNameField');
            startMenu.setTitle(jsonData.data.loginId);
            companyName.setText("<b>"+jsonData.data.companyName+"</b>");
        },
        failure: function(form, action){
               if(action.failureType == Ext.form.Action.CLIENT_INVALID){
                   Ext.Msg.alert("Cannot Submit", "Some fields are still invalid! ");
               }
               if(action.failureType == Ext.form.Action.CONNECT_FAILURE){
                   Ext.Msg.alert("Failure","Server communication failure: "+
                   action.response.status+' '+action.response.statusText);
               }
               if(action.failuretype == Ext.form.Action.SERVER_INVALID){
                   Ext.Mst.alert("Warning", "action.result.errormsg");
               }
         }
      }); 
    },
    
    createWindow: function(extComponent, title, height, width){
        extComponent.region = 'center';
        var win;
        if(!win){
            win = Ext.create('widget.window',{
                title: title,
                layout: 'border',
                closeAction: 'destroy',
                items:[extComponent],
                autoHeight: true,
                width: width,
                modal: false,
                //minimizable: true,
                maximizable: true,
                closable:true,
                collapsible:true,
                animCollapse: true,
                //maximized: true,
                //y: 20,
                listeners: {
                    maximize: function(){
                        var viewportHeight = Ext.getCmp('desktopViewport').height;
                        //Ext.Msg.alert('Message',viewportHeight);
                        win.setHeight(viewportHeight-32);
                        win.doLayout();
                    }
                }
            })
            win.show(this, function(){

            });
        }
    },
    
    refreshWaitingScreenList: function(){
        var task = {
            run: function(){
                var store = null;
                if(Ext.getCmp('waitingScreenTokenGrid') != null){
                    store = Ext.getCmp('waitingScreenTokenGrid').store;
                    //store.load();
                    store.load({
                        params: {},
                        callback: function(r, options, success){
                                if(success == true){
                                    if (r.status == true){     
                                            mask.hide();
                                    }
                                }
                        }
                    });
                }
            },
            interval: 5000 // 2 Second
        }
       Ext.TaskManager.start(task);
    },
    
     refreshDestinationScreenList: function(){
        var task = {
            run: function(){
                var store = null;
                if(Ext.getCmp('destinationScreenTokenGrid') != null){
                    store = Ext.getCmp('destinationScreenTokenGrid').store;
                    //store.load();
                    store.load({
                        params: {},
                        callback: function(r, options, success){
                                if(success == true){
                                    if (r.status == true){     
                                            mask.hide();
                                    }
                                }
                        }
                    });
                }
            },
            interval: 5000 // 2 Second
        }
       Ext.TaskManager.start(task);
    },
    
    initComponent: function() {
        var me = this;
        
        
        
        Ext.applyIf(me, {
            items: [
                {
                    xtype: 'panel',
                    id: 'desktopPanel',
                    //title: 'Desktop',
                    region: 'center',
                    bodyStyle: "background-image:url(../images/background4.jpg)" ,
                    dockedItems: [
                        {
                            xtype: 'toolbar',
                            id: 'desktopBottomToolbar',
                            dock: 'bottom',
                            items: [
                                    {
                                        xtype: 'button',
                                        id: 'startMenuButton',
                                        height: 30,
                                        enableToggle: true,
                                        text: '<b>OPGEA</b>',
                                        //iconCls:'startMenuIcon',
                                        handler: function(){
                                            if(this.pressed == true){
                                                me.startMenuOn();
                                            }
                                            if(this.pressed == false){
                                                me.startMenuOff();
                                            }
                                        }
                                    },
                                    {
                                        xtype: 'tbseparator'
                                    },
                                    {
                                        xtype: 'tbfill'
                                    },
                                    {
                                        xtype: 'button',
                                        id: 'companyNameField',
                                        height: 30,
                                        align: 'right'
                                    }
                            ]
                        }
                    ],
                    items: [
                        {
                        xtype: 'panel',
                        id: 'startMenuPanel',
                        frame: true,
                        height: 310,
                        padding: 0,
                        width: 270,
                        title: 'Shekhar',
                        hidden: true,
                        iconCls: 'userIcon',
                        x: 0,
                        y: 1536,
                        dockedItems: [
                            {
                                xtype: 'toolbar',
                                width: 80,
                                dock: 'right',
                                items: [
                                    {
                                        xtype: 'button',
                                        id: 'startButton',
                                        width: 75,
                                        iconCls: 'settingIcon',
                                        text: 'Settings'
                                    },
                                    '-',
                                    {
                                        xtype: 'button',
                                        width: 75,
                                        iconCls: 'logoutIcon',
                                        text: 'Logout',
                                        handler: function(){
                                            Ext.Ajax.request({
                                                    url	: 'login/logout',
                                                    method	: 'GET',
                                                    success	: function(){
                                                                 Ext.Msg.alert('Confirm', 'Successfully Logout');
                                                                 window.location = 'login';
                                                                },
                                                    failure	: function(){
                                                                 Ext.Msg.alert('Error', 'Failure');
                                                                }			
                                            });

                                        }
                                    }
                                ]
                            }
                        ],
                        items: [
                            {
                                xtype: 'menu',
                                floating: false,
                                height: 295,
                                padding: '0',
                                items: [
                                    {
                                        xtype: 'menuitem',
                                        text: 'Reset Password',
                                        width: 180,
                                        handler: function(){
                                            var resetPassword = Ext.create("Qms.view.login.LoginInfo",{
                                            });
                                            me.createWindow(resetPassword, 'Reset Password', '29%', '40%');
                                            me.startMenuOff();
                                            resetPassword.getLoginInfo();
                                        }
                                    },
                                    {
                                        xtype: 'menuitem',
                                        text: 'Create Destination',
                                        handler: function(){
                                            var destinationGrid = Ext.create("Qms.view.destination.CreateDestination",{
                                            });
                                            me.createWindow(destinationGrid, 'Destination List', '64%', '70%');
                                            me.startMenuOff();
                                        }
                                    },
                                    {
                                        xtype: 'menuitem',
                                        text: 'Create User',
                                        handler: function(){
                                            var employee = Ext.create("Qms.view.employee.EmployeeInfo",{
                                                height: 550
                                            });
                                            me.createWindow(employee, 'User Information', '95%', '85%');
                                            me.startMenuOff();
                                        }
                                    },
                                    '-',
                                    {
                                        xtype: 'menuitem',
                                        text: 'Reception',
                                        handler: function(){
                                            var reception = Ext.create("Qms.view.reception.ReceptionViewport",{
                                                height: 550
                                            });
                                            me.createWindow(reception, 'Reception', '89%', '95%');
                                            me.startMenuOff();
                                        }
                                    },
                                    {
                                        xtype: 'menuitem',
                                        text: 'Destination',
                                        handler: function(){
                                            var destination = Ext.create("Qms.view.destination.DestinationViewport",{
                                                height: me.getViewportHeight()/1.2
                                            });
                                            destination.hideDestinationList();
                                            me.createWindow(destination, 'Destination', '98%', '45%');
                                            me.startMenuOff();
                                        }
                                    },
                                    {
                                        xtype: 'menuitem',
                                        text: 'Waiting Room Screen',
                                        handler: function(){
                                            
                                            var waiting = Ext.create("Qms.view.waiting.WaitingViewport",{
                                                height: me.getViewportHeight()-110
                                            });
                                            me.createWindow(waiting, 'People in Queue', '90%', '85%');
                                            me.startMenuOff();
                                            
                                        }
                                    },
                                    '-',
                                    {
                                        xtype: 'menuitem',
                                        text: 'Video',
                                        handler: function(){
                                            var videoPlayer = Ext.create("Qms.view.video.Video",{
                                                    xtype: 'video',
                                                    id: 'video-player',
                                                    src: [
                                                        // browser will pick the format it likes most:
                                                        { src: 'http://dev.sencha.com/desktopvideo.mp4', type: 'video/mp4' },
                                                        { src: 'http://dev.sencha.com/desktopvideo.ogv', type: 'video/ogg' },
                                                        { src: 'http://dev.sencha.com/desktopvideo.mov', type: 'video/quicktime' }
                                                    ],
                                                    autobuffer: false,
                                                    autoplay : false,
                                                    controls : true
                                                }
                                            );
                                            //videoPlayer.doComponentLayout();
                                            me.createWindow(videoPlayer, 'ExtJS MediaPlayer', '82%', '80%');
                                            me.startMenuOff();
                                            
                                        }
                                    },
                                    {
                                        xtype: 'menuitem',
                                        text: 'Reports'
                                    },
                                    {
                                        xtype: 'menuitem',
                                        text: 'Attendance'
                                    },
                                    {
                                        xtype: 'menuitem',
                                        text: 'Administrator'
                                    }
                                ]
                            }
                        ]
                    }
                    ]
                }
            ]
        });
        me.callParent(arguments);
        var message="Welcome to OPGEA Qms";
        //Ext.Msg.alert('Message', message);
    }
});